﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Compiler
{
    public static class Extension
    {
        public static T To<T>(this object o)
        { 
            return (T)o;
        }

        public static void Pop<T>(this Stack<T> stack, int count)
        {
            for (int i = 0; i < count; ++i)
                stack.Pop();
        }

        //public static List<T> PopList<T>(this Stack<T> stack, int count)
        //{
        //    var final = new List<T>();
        //    for (int i=0; i<count; ++i)
        //        final.Add(stack.Pop());

        //    final.Reverse();
        //    return final;
        //}

        public static T[] PopN<T>(this Stack<T> stack, int count)
        {
            T[] final = new T[count];

            for (int i = count - 1; i >= 0; --i)
                final[i] = stack.Pop();

            return final;
        }

        public static INode[] PopNodes(this Stack<INode> stack, int count)
        {
            INode[] final = new INode[count];

            for (int i = count - 1; i >= 0; --i)
                final[i] = stack.Pop();

            return final;
        }

        //public static List<T> DequeueList<T>(this Queue<T> queue, int count)
        //{
        //    var final = new List<T>();
        //    for (int i = 0; i < count; ++i)
        //        final.Add(queue.Dequeue());

        //    return final;
        //}

        public static void print<T>(this Stack<T> stack)
        {
            Stack<T> cpy = new Stack<T>();
            int cnt = stack.Count;
            for (int i = 0; i < cnt; ++i)
            {
                cpy.Push(stack.Pop());
            }

            Console.Write("stack ");
            for (int i = 0; i < cnt; ++i)
            {
                var e = cpy.Pop();
                Console.Write(e.ToString() + " ");
                stack.Push(e);
            }
            Console.WriteLine();
        }
    }
}
